package xin.clips.quartz.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xin.clips.common.utils.DateUtils;
import xin.clips.common.utils.StringUtils;
import xin.clips.system.domain.gy.*;
import xin.clips.system.mapper.gy.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 每天插入今天机床的运行时间和产量
 * 
 * @author zhushanglin
 */
@Component("machineDayRuntimeTask")
public class MachineDayRuntimeTask {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());


    @Autowired
    private GyMachineDayRuntimeMapper machineDayRuntimeMapper;

    @Autowired
    private GyMachineParamMapper machineParamMapper;

    @Autowired
    private GyDeviceSourceMapper deviceSourceMapper;


    public void perDay() {
        List<GyMachineDayRuntime> machineRuntime = new ArrayList<>();

        List<GyDeviceSource> allDeviceSources = deviceSourceMapper.getAllDeviceSources();
        for(int i=0; i<allDeviceSources.size(); i++){
            if("machine001-process_time".equals(allDeviceSources.get(i).getName())){
                GyMachineDayRuntime gyMachineDayRuntime = new GyMachineDayRuntime();
                if(allDeviceSources.get(i).getValue().longValue()==0){
                    List<GyMachineDayRuntime> machineList = machineDayRuntimeMapper.getMachineLastRuntime();
                    Map<String, GyMachineDayRuntime> map = machineList.stream().collect(Collectors.toMap(GyMachineDayRuntime::getDeviceName, item -> item));
                    GyMachineDayRuntime machine = map.get("机床001");
                    gyMachineDayRuntime.setRunTime(machine.getRunTime());
                }else {
                    gyMachineDayRuntime.setRunTime(allDeviceSources.get(i).getValue().longValue()+"");
                }

                gyMachineDayRuntime.setDeviceName("机床001");
                gyMachineDayRuntime.setLocationId(3);
                machineRuntime.add(gyMachineDayRuntime);
            }
            if("machine002-process_time".equals(allDeviceSources.get(i).getName())){
                GyMachineDayRuntime gyMachineDayRuntime = new GyMachineDayRuntime();
                if(allDeviceSources.get(i).getValue().longValue()==0){
                    List<GyMachineDayRuntime> machineList = machineDayRuntimeMapper.getMachineLastRuntime();
                    Map<String, GyMachineDayRuntime> map = machineList.stream().collect(Collectors.toMap(GyMachineDayRuntime::getDeviceName, item -> item));
                    GyMachineDayRuntime machine = map.get("机床002");
                    gyMachineDayRuntime.setRunTime(machine.getRunTime());
                }else {
                    gyMachineDayRuntime.setRunTime(allDeviceSources.get(i).getValue().longValue() + "");
                }
                gyMachineDayRuntime.setDeviceName("机床002");
                gyMachineDayRuntime.setLocationId(3);
                machineRuntime.add(gyMachineDayRuntime);
            }
        }

        machineDayRuntimeMapper.insertGyMachineDayRuntime(machineRuntime);


    }

    public void perDayProcttion() {
        List<GyMachineDayProduction> machineProduction = machineParamMapper.getMachineProduction();

        machineDayRuntimeMapper.insertGyMachineDayProduction(machineProduction);


    }


}
